#include "NestPathInfoDialog.h"
#include "ui_NestPathInfoDialog.h"
#include "utils/GeometryUtils.h"
#include "utils/SVGUtil.hpp"
#include "QGraphicsPolygonItem"
#include "QDebug"

NestPathInfoDialog::NestPathInfoDialog(NestPath* bin, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NestPathInfoDialog),
    m_bin(bin)
{
    ui->setupUi(this);
    this->initDialog();
    connect(ui->button_close_dialog, &QPushButton::clicked, this, &NestPathInfoDialog::closeDialog);
    this->setWindowTitle("info");
}

NestPathInfoDialog::~NestPathInfoDialog()
{
    qDebug()<<"析构对话框";
    delete ui;
}

void NestPathInfoDialog::initDialog(){
    NestPath bin = *(this->m_bin);
    std::vector<double> wh = GeometryUtils::getBoundingBoxSize(bin);
    this->ui->bin_width->setText(QString::number(wh[0]));
    this->ui->bin_hight->setText(QString::number(wh[1]));
    this->ui->bin_width->setStyleSheet("QLineEdit { background-color: #CCCCCC; }");
    this->ui->bin_hight->setStyleSheet("QLineEdit { background-color: #CCCCCC; }");
    this->ui->path_name->setText(QString::fromStdString(bin.name));
    this->ui->inflate_delta->setValue(0.0);

    QGraphicsScene *scene = new QGraphicsScene();
    //    scene->setSceneRect(0,0,ui->nestOutputView->width(), ui->nestOutputView->height());
    for (int i = 0; i < bin.size(); i++) {
        std::vector<double> x=std::vector<double>();
        std::vector<double> y=std::vector<double>();
        Polygon *polygon=bin[i];
        SVGUtil::buildPolygonVector(polygon,x,y);
        // Convert x,y vectors to a QPolygonF
        QPolygonF poly;
        for (int k = 0; k < x.size(); k++) {
            poly << QPointF(x[k],y[k]);
        }
        // Create a QGraphicsPolygonItem with the polygon shape
        QGraphicsPolygonItem *item = new QGraphicsPolygonItem(poly);
        scene->addItem(item);
    }

    ui->graphicsView->setScene(scene);
}

void NestPathInfoDialog::closeDialog()
{
    qDebug()<<"关闭对话框";
    this->close();
}


void NestPathInfoDialog::on_button_confirm_clicked()
{
    this->m_bin->name = this->ui->path_name->text().toStdString();
    this->close();
}

